<?php
namespace app\common\model;

use app\admin\model\SubscribeOrder as orderModel;
use app\admin\model\SubscribeRecord as recordModel;
use app\common\model\Upay;
use app\common\model\User;
use think\Exception;
use think\facade\Db;
use ball\WechatApp;
/**
 * SubscribeOrder
 */
class SubscribeOrder 
{
    public static function subscribe(array $goods,array $user = []){
        $isopen = get_sys_config('is_open');
        $data['pay_mode'] = get_sys_config('pay_mode');
        $data['type'] = $goods['order_type'];
        $data['out_trade_type'] = 6;
        $data['title'] = '场馆预约'.$goods['times'].'小时';
        $data['out_trade_no'] = date('ymd', time()) . mt_rand(1000, 9999) . substr(time(), '3', '8');
        $data['order_amount'] = $goods['order_amount'];
        $data['user_id'] = $user['id'];
        $data['goods'] = json_encode($goods['record'],JSON_UNESCAPED_UNICODE);
        Db::startTrans();
        try {
            $order = orderModel::create($data);
            if($order->type == 1){
                $order->pay_status = 2;
                $order->state = $isopen=='1'?1:2;
                $order->status = 1;
                $order->save();
                $rlt = ['status'=>'paid'];
                User::where('id',$user['id'])->setInc('subscribe_time_long',$goods['times']);
                Db::name('UserLog')->save(['type'=>3,'user_id'=>$order->user_id,'content'=>'场馆预约'.$goods['times'].'小时']);
            }else{
                $rlt = Payment::payment($order,$user);
                if($rlt === false){
                    throw new Exception("生成订单失败");
                }
            }
            $record = [];
            
            foreach ($goods['record'] as $key => $value) {
                $record[]=[
                    'order_id'=>$order->id,
                    'stadium_id'=>$value['stadium_id'],
                    'user_id'=>$user['id'],
                    'subscribe_type'=>1,
                    'subscribe_time'=>strtotime($value["day"].' '.$value['hour'].':00:00'),
                    'subscribe_time_long'=>1,
                    'year'=>date('Y',strtotime($value["day"])),
                    'month'=>date('m',strtotime($value["day"])),
                    'days'=>date('d',strtotime($value["day"])),
                    'hour'=>$value['hour'],
                    'state'=>$isopen=='1'?2:3,
                    'status'=>$data['type']
                ];
            }
            $subrcord = new recordModel();
            $subrcord->saveAll($record);
            // 提交事务
            Db::commit();
            
            return ['order'=>$order->toArray(),'payment'=>$rlt];
        } catch (Exception $e) {
            // 回滚事务
            Db::rollback();
            return false;
        }
    }
    public static function createOrder(array $goods = [],array $user = []){
        $data['pay_mode'] = get_sys_config('pay_mode');
        $data['type'] = $goods['order_type'];
        switch ($goods['type']) {
            case 'once_big':
                $data['out_trade_type'] = 1;
                break;
            
            case 'once_small':
                $data['out_trade_type'] = 2;
                break;
            case 'vip_card_M':
                $data['out_trade_type'] = 3;
                break;
            
            case 'vip_card_Q':
                $data['out_trade_type'] = 4;
                break;
            case 'vip_card_Y':
                $data['out_trade_type'] = 5;
                break;
            
            default:
                $data['out_trade_type'] = 6;
                break;
                
        }
        $data['title'] = $goods['title'];
        $data['out_trade_no'] = date('ymd', time()) . mt_rand(1000, 9999) . substr(time(), '3', '8');
        $data['order_amount'] = $goods['price'];
        $data['user_id'] = $user['id'];
        $data['goods'] = json_encode($goods,JSON_UNESCAPED_UNICODE);
        // 启动事务
        Db::startTrans();
        try {
            $order = orderModel::create($data);
            $rlt = Payment::payment($order,$user);
            if($rlt === false){
                throw new Exception("生成订单失败");
            }
            // 提交事务
            Db::commit();
            
            return ['order'=>$order->toArray(),'payment'=>$rlt];
        } catch (Exception $e) {
            // 回滚事务
            Db::rollback();
            return false;
        }
    }
    /**
     * 验证收钱吧支付订单
     */
    public static function verifyOrder(){

    }

}